/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.passport.service.plugins  
 */
package com.apache.passport.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.database.constant.SystemTools;
import com.apache.database.constant.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.passport.entity.LoginInfo;
import com.apache.passport.entity.Token;
import com.apache.passport.manager.LoginInfoManager;

/**
 * description:  获取用户上次登录信息(支持外部系统调用,因内部系统直接调会简单点)
 * @author Hou Dayu 创建时间：2014-12-26  
 * @Copyright(c)2014:
 */
public class LastMsgPlugin implements PluginConnector {
	protected IDao loginInfoDao;
	private LoginInfoManager loginInfoManager;

	public void setLoginInfoDao(IDao loginInfoDao) {
		this.loginInfoDao = loginInfoDao;
	}

	public void setLoginInfoManager(LoginInfoManager loginInfoManager) {
		this.loginInfoManager = loginInfoManager;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)  
	 */
	@Override
	public Object execute(ParamsVo arg0) throws Exception {
		ResultEntity result = new ResultEntity();
		result.setResult("true");
		String toeknId = (String) arg0.getParams("tokenId");
		String userEname = (String) arg0.getParams("userEname");
		if (Validator.isNull(toeknId)) {
			result.setMessage("缺少必要参数:[toeknId]");
			result.setEntity("F");
			return result;
		}
		if (Validator.isNull(toeknId)) {
			result.setMessage("缺少必要参数:[userEname]");
			result.setEntity("F");
			return result;
		}
		Token token = (Token) SystemTools.getInstance().getCache("loginToken").getCacheCloneByKey(toeknId);
		if (Validator.isEmpty(token)) {
			result.setMessage("token已过期或不存在");
			result.setEntity("F");
			return result;
		}

		LoginInfo loginInfos = (LoginInfo) getVoByEname(userEname);

		if (Validator.isEmpty(loginInfos)) {
			result.setMessage("查询用户信息失败,原因或许为用户名不存在");
			result.setEntity("F");
			return result;
		}

		LoginInfo loginInfo = new LoginInfo();
		loginInfo.setLgnJudge(loginInfos.getLgnJudge());
		loginInfo.setLgnLastarea(loginInfos.getLgnLastarea());
		loginInfo.setLgnLastdevice(loginInfos.getLgnLastdevice());
		loginInfo.setLgnLastipaddress(loginInfos.getLgnLastipaddress());
		loginInfo.setLgnLastsys(loginInfos.getLgnLastsys());
		loginInfo.setLgnLasttime(loginInfos.getLgnLasttime());
		loginInfo.setSysEname(loginInfos.getSysEname());
		loginInfo.setUserEname(userEname);

		result.setMessage("查询用户最后登录信息成功");
		result.setEntity(loginInfo);
		return result;
	}

	/**
	 * description:  根据用户名去数据库查询用户最后一次的登录信息
	 * @param userEname
	 * @return  
	 * @author Hou Dayu 
	 * @update 2014-12-26
	 */
	public Object getVoByEname(String userEname) {
		String sql = loginInfoDao.getSql(3);
		MethodParam param = new MethodParam("ById", "", sql, "com.apache.passport.entity.LoginInfo");
		param.setParams("userEname", userEname);
		return loginInfoDao.selectSingle(param);
	}

}
